package com.btlib.lib;

import android.content.Intent;
import android.util.Log;

public class CarControl {
  public final static int TURN_LEFT = 0;
  public final static int TURN_RIGHT = 1;
  public final static int FOWARD = 2;
  public final static int BACK = 3;
  public final static int STOP = 4;
  private static final int DISCONNECT = 7;
  private static final int SENSORROTATE = 9;
  private static final int DISTANCEREQ = 10;
  public static final byte DEFSPEED = 45;
  private BluetoothControl btControl;
  private byte[] readData;
  private final int DataLen = 8;
  private final int DataOff = 4;
  public CarControl(MyActivity context){
    btControl = new BluetoothControl(context);
    readData = new byte[DataLen];
    //btControl.init();
  }
  public void controlLeft(byte speed){
    btControl.writeMessage(new byte[]{TURN_LEFT, speed});
    btControl.readMessage(readData);
  }
  public void controlRight(byte speed){
    btControl.writeMessage(new byte[]{TURN_RIGHT, speed});
    btControl.readMessage(readData);
  }
  public void controlFoward(byte speed){
    btControl.writeMessage(new byte[]{FOWARD, speed});
    btControl.readMessage(readData);
  }
  public void controlBackword(byte speed){
    btControl.writeMessage(new byte[]{BACK, speed});
    btControl.readMessage(readData);
  }
  public void controlDisconnect(){
    btControl.writeMessage(new byte[]{DISCONNECT, 0});
    btControl.readMessage(readData);
  }
  public void controlStop(){
    btControl.writeMessage(new byte[]{STOP, 0});
    btControl.readMessage(readData);
  }
  public boolean controlStart(){
    btControl.init();
    //onWait();
    return btControl.isConnected();
  }
  public void controlSensor(byte angle){
    btControl.writeMessage(new byte[]{SENSORROTATE, angle});
    btControl.readMessage(readData);
  }
  public int getDistance(){
    btControl.writeMessage(new byte[]{DISTANCEREQ});
    //btControl.readMessage(readData);
    /*Log.e("CarControl ",readData[0]+" "+
        readData[1]+" "+readData[2]+" "+
        readData[3]+" "+readData[4]+" "+
        readData[5]+" "+readData[6]);*/
    //int dis = readData[DataOff]*100+readData[DataOff+1]*10+readData[DataOff+2];
    int dis = btControl.readMessageInt();
    Log.e("CarControl","distance = "+dis);
    return dis;
  }
  
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    btControl.onActivityResult(requestCode, resultCode, data);
  }
  
  public void onDestory() {
    // TODO Auto-generated method stub
    btControl.onDestory();
  }
  /*@Override
  public void onSignal() {
    // TODO Auto-generated method stub
    btControl.onSignal();
  }
  @Override
  public void onWait() {
    // TODO Auto-generated method stub
    btControl.onWait();
  }*/
}
